package store.lunangangster.nodeflow.parser.el.operator;

import static store.lunangangster.nodeflow.enums.FlowType.FLOW_FOR;

import cn.hutool.core.collection.CollUtil;
import com.ql.util.express.Operator;
import store.lunangangster.nodeflow.builder.FlowBuilder;
import store.lunangangster.nodeflow.core.component.ForComponent;
import store.lunangangster.nodeflow.exception.RuleSourceParseException;
import store.lunangangster.nodeflow.parser.el.OperatorHelper;
import store.lunangangster.nodeflow.rt.element.Node;
import store.lunangangster.nodeflow.util.Converter;

/**
 * For表达式所关联的操作符
 *
 * @author cwk
 * @version 1.0
 */
public class ForOperator extends Operator {

  @Override
  public Object executeInner(Object[] list) throws RuleSourceParseException {
    OperatorHelper.checkArguments(list);

    Node forNode = Converter.convert(list[0], Node.class);
    if (!forNode.isComponent(ForComponent.class)) {
      throw new RuleSourceParseException("Incompatible executable for ForFlow: should be like FOR(forNode).DO(flow) or FOR(forNode).DO(flow).EXIT(exitNode)");
    }
    return FlowBuilder.buildELFLow(FLOW_FOR, CollUtil.toList(forNode));
  }
}
